/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function questionSearchDone(data){
    if(parseInt(data.hits) > 0){
        $("#question-hits-count").html('('+data.hits+')');
        var output = '';
        if(data.question instanceof Array){
            $.each(data.question, function(i,questionItem){
                //console.log('loop ' + i);
                output +=  renderQuestion(questionItem, true);
            });
        }else{
            //console.log('single hit');
            output +=  renderQuestion(data.question, true);
        }        
        
        $("#question-result").html(output);
        
        $("#question-result .referring").click(function(event){
            var link = $(this);
            var id   = $(link).attr('data-id');
            var type = $(link).attr('data-type');
             
            link.addClass('loading');
            
            var data = {
                q:id, 
                type:type,
                format:'json'
            };
            
            $.ajax({
                url: settings.backend + '/ws/referring.xql',
                dataType: 'jsonp',
                data: data,
                success: function(result){
                    var studies = '';
                    if(result.enteties.study instanceof Array)
                        studies = renderStudyTable(result.enteties.study);
                    else
                        studies = renderStudyTable([result.enteties.study]);
                    $(link).prev().html(studies);
                    $(link).prev().find('ul li').sortElements(function(a, b){
                        return $(a).text() > $(b).text() ? 1 : -1;
                    });
                    
                    setActiveLang(settings.active_lang);
                    
                    link.removeClass('loading'); 
                    link.hide();
                }
            });
            
            event.preventDefault();
        });
        
        

    }else{
        $("#question-hits-count").html('');
        $("#question-result").html('');
    }

    $('.question-tab a').removeClass('loading');    
}

function renderStudyTable(studies){
    var output = '<ul>';
    
    var series = '';
    
    $.each(studies, function(i,s){
        /*if(s.series != null && series != s.series.en){
            if(series != '')
                output += '</ul>';
            
            series = s.series.en;
            //console.log(series);
            
            output += $.format('<li class="series">%s</li><ul>', [series]);
        }*/
        //console.log('  '+s.title.en);
        output += '<li class="study"><p class="lang-strings">';
        $.each(s.title, function(i,title){
            output += $.format('<span class="%s"><a href="http://snd.gu.se/%s/catalogue/study/%s">%s</a></span>', [i, i, s.id, title]);
        });
        output += '</p></li>';
    });
    
    output += '</ul>';
    //console.log(output);
    return output;
}


function renderQuestion(questionItem, toplevel){

    var output = '';
    var qid = questionItem.id;
    var question_id = questionItem.id; 
    
    var name = '';
    if(questionItem.name)
        name = questionItem.name;
    
    question_id = 'asdasd';
    
    if(questionItem.userid){
        $.each(questionItem.userid.id, function(i,id){
             if(id.type == "question_id")
                 question_id = id['#text'];
        });
    }
    
    /*
    if(questionItem.UserID instanceof Array)
        question_id = questionItem.UserID[0]['#text'];
    else if(questionItem.UserID['#text'])
        question_id = questionItem.UserID['#text'];
    */
   
    if(toplevel)
        output += '<div id="'+qid+'" class="question-block"><ul class="top-level">';
    else
        output += '<ul id="'+qid+'" class="question-sub">';
    
    output += $.format('<li class="question-icon"><span class="question-name">%s</span><p class="lang-strings">', [name]);

    $.each(questionItem.questiontext, function(i,questionText){
        output += $.format('<span class="%s">%s</span>', [i,questionText]);
    });
    output += '</p></li>';
    
    //subquestions
    if(questionItem.subquestions){
        if(questionItem.subquestions.question instanceof Array){
            $.each(questionItem.subquestions.question, function(i,qi){
                output +=  renderQuestion(qi);
            });
        }else if(questionItem.subquestions.question){
            output +=  renderQuestion(questionItem.subquestions.question);
        }
    }

    if(toplevel && questionItem.userid){
        output += '</ul><div class="studies"><ul><li class="study"><p class="lang-strings">';
        $.each(questionItem.study.title, function(i,title){
            output += $.format('<span class="%s"><a href="http://snd.gu.se/%s/catalogue/study/%s">%s</a></span>', [i, i, questionItem.study.id, title]);
        });
        output += '</p></li></ul></div>';
        
        output += $.format('<a data-id="%s" data-type="question_id" href="#" class="question-info referring" data-localize="get_studies">%s</a></div>', [question_id, getLocaleString('get_studies')]);
    }else{
        output += '</ul>';
    }

    return output;
}